<?php
//验证类
class lib_validate{
	//验证码
	public static function verifycode(){
		include_once 'application/classes/common/verifycode.php';
	}
	
	//字符串格式验证
	public static function preg($str,$type = null){
		switch ($type){
			case 'url':
				$format = '/^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\/?%&=]*)?$/i';
				break;
			case 'email':
				$format = '/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/';
				break;
			default:return false;
		}
		return (bool)preg_match($format,$str);
	}
	
	//检查数组或对象中必须的元素是否存在
	//lib_validate::need($post,array('name','power'=>'缺少权限定义'));
	public static function need($target,$needs){
		if (!is_object($target) && !is_array($target)){
			lib_message::message('无效对象');
			return false;
		}
		if (is_object($target)) $target = (array)$target;
		if (empty($target)){
			lib_message::message('参数列表为空');
			return false;
		}
		
		$message_pre = '缺少必要的参数:';
		foreach ($needs as $need_k => $need_v){
			if (is_string($need_k)){
				$message = $need_v;
				$need_v = $need_k;
			}else{
				$message = $message_pre.$need_v;
			}
			if (!isset($target[$need_v]) || (is_string($target[$need_v]) && !strlen($target[$need_v]))){
				lib_message::message($message);
				return false;
			}
		}
		return true;
	}
}